package com.pswv2.withdraw.imp;


import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.pswv2.bub.bsc.dao.po.AccountDetails;
import com.pswv2.bub.bsc.dao.po.MyWallet;
import com.pswv2.bub.bsc.dao.po.Withdraw;
import com.pswv2.util.AlipayWithdraw;
import com.pswv2.util.ExtraSpringHibernateTemplate;
import com.pswv2.withdraw.service.WithdrawService;

@Service
@SuppressWarnings("unchecked")
public class WithdrawImpl implements WithdrawService {
	private static Class<?> poc = Withdraw.class;
	@Autowired
	private ExtraSpringHibernateTemplate extraSpringHibernateTemplate;
	/**
	 * 用户提现
	 */
	@Override
	public Integer alipayWithdraw(double money,Integer userId,String alipayAccount) {
		// TODO Auto-generated method stub
		AlipayWithdraw w=new AlipayWithdraw();
		MyWallet entity=extraSpringHibernateTemplate.findFirstOneByPropEq(MyWallet.class, "userId", userId);
		Integer s=w.withdraw(money,userId,alipayAccount);
		if(s==1){
			double balance=entity.getMoney();
			entity.setMoney(balance-money);
			if(entity.getAlipayAccount()==null){
				entity.setAlipayAccount(alipayAccount);
			}else if(!entity.getAlipayAccount().equals(alipayAccount)){
				entity.setAlipayAccount(alipayAccount);
			}
			extraSpringHibernateTemplate.getHibernateTemplate().update(entity);
			AccountDetails entityAD=new AccountDetails();
			entityAD.setAmount(money);
			entityAD.setTime(new Date().getTime());
			entityAD.setMatter("我的提现");
			entityAD.setUserId(userId);
			entityAD.setProceedsPaid(0);
			extraSpringHibernateTemplate.getHibernateTemplate().save(entityAD);
			return s;
		}else{
			return s;
		}
	}
	/**
	 * 通过用户提现Id查询
	 */
	@Override
	public Withdraw queryById(Integer withdrawId) {
		// TODO Auto-generated method stub
		Withdraw entity=extraSpringHibernateTemplate.findFirstOneByPropEq(poc, "withdrawId", withdrawId);
		return entity;
	}
	/**
	 * 添加提现
	 */
	@Override
	public void add(double money, Integer userId, String alipayName, String alipayAccount) {
		// TODO Auto-generated method stub
		Withdraw entity=new Withdraw();
		entity.setMoney(money);
		entity.setUserId(userId);
		entity.setAliapyAccount(alipayAccount);
		entity.setAliapyName(alipayName);
		entity.setTime(new Date().getTime());
		entity.setState(0);
		extraSpringHibernateTemplate.getHibernateTemplate().save(entity);
		
	}
	/**
	 * 通过id修改提现状态
	 */
	@Override
	public void editById(Integer withdrawId, Integer state) {
		// TODO Auto-generated method stub
		Withdraw entity=extraSpringHibernateTemplate.findFirstOneByPropEq(poc, "withdrawId", withdrawId);
		entity.setState(state);
		extraSpringHibernateTemplate.getHibernateTemplate().update(entity);
	}

	
	@Override
	public List<Withdraw> queryAll() {
		// TODO Auto-generated method stub
		List<Withdraw> ls=(List<Withdraw>) extraSpringHibernateTemplate.findAll(poc);
		return ls;
	}

	@Override
	public List<Withdraw> queryByKey(String key) {
		// TODO Auto-generated method stub
		List<Withdraw> ls=(List<Withdraw>) extraSpringHibernateTemplate.getHibernateTemplate().find("", key);
		return ls;
	}

	@Override
	public List<Withdraw> queryByState(Integer state) {
		// TODO Auto-generated method stub
		List<Withdraw> ls=(List<Withdraw>) extraSpringHibernateTemplate.getHibernateTemplate().find("", state);
		return ls;
	}

}
